(function(){
    var // constants
        CHAR_START  = '◄',
        CHAR_END    = '►',
        CHAR_STARTRegExp = new RegExp(Ext.String.escapeRegex(CHAR_START), 'g'),
        CHAR_ENDRegExp   = new RegExp(Ext.String.escapeRegex(CHAR_END), 'g'),

        // diacritics RegExp
        CHAR_ESCAPE_A_REG_EXP = /([aâă])/gi,
        CHAR_ESCAPE_I_REG_EXP = /([iî])/gi,
        CHAR_ESCAPE_S_REG_EXP = /([sş])/gi,
        CHAR_ESCAPE_T_REG_EXP = /([tţ])/gi;


    Ext.define('Bible.util.Format', {

        singleton: true,

        /**
         * Escapes diacritics in the passed string for use in a regular expression
         * @param {String} s
         * @return {String}
         */
        escapeDiacriticsRe : function(s){
            return s.replace(CHAR_ESCAPE_A_REG_EXP, "[aâă]").
                replace(CHAR_ESCAPE_I_REG_EXP, "[iî]").
                replace(CHAR_ESCAPE_S_REG_EXP, "[sş]").
                replace(CHAR_ESCAPE_T_REG_EXP, "[tţ]");
        },

        /**
         * @param {String} val
         * @param {String/Array} search a String or Array of Strings
         * @param {Boolean} split
         * @return {String} val highlighted value
         */
        highlightText : function(val, search, split, minChars, showKeywords) {
            var //KeywordsStore = Bible.data.KeywordsStore,
                            re = [].concat(search);

            minChars = minChars || 1;
            if(split && Ext.isString(search)){
                re = search.split(' ');
            }

//            if(showKeywords){
//                Ext.each(KeywordsStore.keywordsReplace, function(e, i){
//                    val = val.replace(e.replaceReg, e.replaceText);
//                });
//            }

            // highlight
            for(var j=0; j<re.length; j++) {
                if(re[j].length < minChars){
                    continue;
                }
                var s = this.escapeDiacriticsRe(Ext.String.escapeRegex(re[j])),
                        regExp = new RegExp('(' + s + ')', 'gi');
                val = val.replace(regExp, CHAR_START + "$1" + CHAR_END);
            }

//            if(showKeywords){
//                val = String.format.apply(window, [val].concat(KeywordsStore.keywordsSpan));
//            }
            val = val.replace(CHAR_STARTRegExp, '<span class="highlight">');
            val = val.replace(CHAR_ENDRegExp,   '</span>');
            return val;
        }
    });
})();